package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.exception.CustomException;
import cn.cnf.entity.vo.CommentInfo;
import cn.cnf.service.CommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务控制器
 *
 * @author ZhuMeiYi
 * @description
 * @since 2020-12-04 15:18:43
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 增加评论
     *
     * @param comment
     * @return
     */
    @PostMapping("/add")
    public Result addComment(@RequestBody CommentInfo comment) {
        try {
            return Result.success(commentService.addComment(comment));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 删除评论
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result deleteComment(@RequestParam String id) {
        try {
            return Result.success(commentService.delete(id));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 查询全部评论
     *
     * @param fromId
     * @return
     */
    @GetMapping("/all")
    public Result getAllComments(@RequestParam String fromId) {
        try {
            return Result.success(commentService.getAllComments(fromId));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

}